package com.autobet.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.autobet.dao.CommonDao;
import com.autobet.model.support.Visualization;
import com.autobet.model.table.Account;
import com.autobet.model.table.AutoHis;
import com.autobet.services.*;
import com.autobet.utils.BetUtils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.*;

@Controller
public class CarStatusController
{
    private static final Logger log = LoggerFactory.getLogger(CarStatusController.class);

    @Autowired
    private CommonDao commonDao;

    @RequestMapping(
            value = {"/setCarStaus"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject setCarStaus(String type , String NO , boolean carStatus ) {
        JSONObject jsonObject = new JSONObject();
        try {
            CarStatus.setCarStatus( type , NO , carStatus);
            jsonObject.put("status" , "success");
            if(CarStatus.getCarStatus(type , NO)){
                jsonObject.put("changeNow" , "启用");
            }else{
                jsonObject.put("changeNow" , "停用");
            }

            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }
    }

    /**
     * 获取开车状态
     * @return
     */
    @RequestMapping(
            value = {"/getCarStatusAll"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject getCarStatusAll() {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("status" , "success");
            jsonObject.put("data" , CarStatus.getCarStatusAll());
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }


    /**
     * 获取当前穿版次数
     * @return
     */
    @RequestMapping(
            value = {"/getDieCount"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject getDieCount() {
        BigDecimal winAmount;
        JSONObject jsonObject = new JSONObject();
        try {
            winAmount = this.commonDao.queryMaxDieCountAutoHis(BetUtils.getCurrDateYYYYMMDD());
            jsonObject.put("status" , "success");
            jsonObject.put("data" , winAmount);
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }

    /**
     * 清空穿版
     * @return
     */
    @RequestMapping(
            value = {"/clearDieCount"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject clearDieCount() {

        JSONObject jsonObject = new JSONObject();
        try {

            for( int i= 1 ;i<= 10 ;i++){
//                BetStatus.setPreAmount("dx" ,String.valueOf(i),new BigDecimal("1"));
//                BetStatus.setBuyAmount("dx" ,String.valueOf(i),new BigDecimal("1"));
                BetStatus.setDieCount("dx" ,String.valueOf(i),new BigDecimal("0"));
//                BetStatus.setPreAmount("ds" ,String.valueOf(i),new BigDecimal("1"));
//                BetStatus.setBuyAmount("ds" ,String.valueOf(i),new BigDecimal("1"));
                BetStatus.setDieCount("ds" ,String.valueOf(i),new BigDecimal("0"));
            }
            this.commonDao.clearDieCount();
            EnvConfigService.clearFlag = true;
            jsonObject.put("status" , "success");
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }

    /**
     * 获取最新一期数据
     * @return
     */
    @RequestMapping(
            value = {"/getNewIDData"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject getNewIDData() {

        JSONObject jsonObject = new JSONObject();
        try {
//            List<AutoHis> autoHisList =this.commonDao.getNewIDData();

            JSONArray data = BetStatus.getAllStatus();
            jsonObject.put("status" , "success");
            jsonObject.put("data" , data);
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }

    /**
     * 获取单车数据
     * @return
     */
    @RequestMapping(
            value = {"/getNewIDDataOne"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject getNewIDDataOne( String type , String NO  ) {

        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject data = new JSONObject();
            data.put("type", type);
            data.put("NO", NO);
            data.put("dieCount" , BetStatus.getDieCount(type , NO));
            data.put("preAmount" , BetStatus.getPreAmount(type , NO));
//            AutoHis autoHis =this.commonDao.getNewIDDataOne( type , NO);

            jsonObject.put("status" , "success");
            jsonObject.put("data" , data);
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }

    /**
     * 设置车新数据
     * @param type
     * @param NO
     * @return
     */
    @RequestMapping(
            value = {"/setCarData"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject setCarData( String type , String NO , String dieCount , BigDecimal preAmount ) {

        JSONObject jsonObject = new JSONObject();
        try {
            BetStatus.setBuyAmount(type ,NO,preAmount);
            BetStatus.setPreAmount(type ,NO,preAmount);
            this.commonDao.setCarData( type , NO ,dieCount);
            SetCarStatus.setCarStatus( type ,NO ,true );
            jsonObject.put("status" , "success");
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }


    /**
     * 获取每期盈亏
     * @return
     */
    @RequestMapping(
            value = {"/getVisualizationData"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject getVisualizationData( String type , String NO  ) {

        JSONObject jsonObject = new JSONObject();
        try {
            List<Visualization>  visualizationList =this.commonDao.queryVisualizationData( );
            jsonObject.put("status" , "success");
            jsonObject.put("data" , visualizationList);
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }

    /**
     * 获取每期盈亏统计
     * @return
     */
    @RequestMapping(
            value = {"/getVisualizationDataSum"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject getVisualizationDataSum( String type , String NO  ) {

        JSONObject jsonObject = new JSONObject();
        try {
            List<Visualization>  visualizationList =this.commonDao.queryVisualizationDataSum( );
            jsonObject.put("status" , "success");
            jsonObject.put("data" , visualizationList);
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }

    /**
     * 设置反转下注
     * @param type
     * @param NO
     * @return
     */
    @RequestMapping(
            value = {"/setReversal"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public JSONObject setReversal( String type , String NO  ) {

        JSONObject jsonObject = new JSONObject();
        try {
            if(EnvConfigService.reversal){
                EnvConfigService.reversal =false;
            }else{
                EnvConfigService.reversal =true;
            }
            jsonObject.put("status" , "success");
            jsonObject.put("data" , EnvConfigService.reversal);
            return jsonObject;
        } catch (Exception var3) {
            jsonObject.put("status" , "failed");
            jsonObject.put("message" , var3.getMessage());
            return jsonObject;
        }

    }


}
